$(document).ready(function(){
	var $form = $('#reg-form'),
		$bcInput = $('.bc-input'),
		$uid = $('#uid'),
		$email = $('#email');
		$password = $('#password'),
		$confirmpsw = $('#confirmpsw'),
		$rand = $('#rand'),
		$myImage = $('.myImage'),
		$myImageRefresh = $('.myImage-refresh'),
		tipsPic = $('.modal .tips-pic')[0],
		$tips = $('.modal .tips-content'),
		$dialogBox = $('#dialog-box'),
		$regBtn = $('.reg-btn');
		
	//focusCheck函数
	var focusCheck = function(){
		var me = this ,
			$bcInput = $(me).closest('.bc-input'),
			tipsText = '';
		if(me.id=='uid'){
			tipsText = ' 2-20个字母、数字或"_"';
		}else if(me.id=='password'){
			tipsText = ' 6-20个字符';
		}else if(me.id=="email"){
			tipsText = ' 例如: book@163.com';
		}
		if($bcInput.children('.check-tips').length==0){
			$bcInput.append('<div class="col-sm-4 sm-padding check-tips">'+
								'<span class="tips-pic pic-info"></span><span class="tips-text">'+tipsText+'</span>'+
							'</div>');
		}else{
			$bcInput.find('.tips-pic')[0].className='tips-pic pic-info';
			$bcInput.find('.tips-text').html(tipsText);
		}
	}
	//用户名校验的函数
	var uidBlurCheck = function(){
		var me = this,
			$bcInput = $(me).closest('.bc-input');
		if($bcInput.children('.check-tips').length==0){
			focusCheck.call(me);
		}
		var	$tipsPic = $bcInput.find('.tips-pic'),
			$tipsText = $bcInput.find('.tips-text');
			val = $uid.val();
		//如果用户名存在前后空格，去除空格
		if(val!='' && /^\s|\s$/g.test(val)){
			$uid.val(val = $.trim(val));
		}
		if(val!=''){
			//检测用户名是否合格
			if(BC.checkUserName(val)){
				//判断用户名是否可用且是否跟上一次相同
				if(!me.val || me.val != val){
					//显示正在加载图标
					$tipsPic[0].className = 'loading-pic rel-top-3';
					$tipsText.html('');
					//检测用户名是否已存在
					BC.checkUserExecute({
						uid:val,
						s:function(){
							$tipsPic[0].className='tips-pic pic-error';
							$tipsText.html(' 该用户名已被注册');
							this.isSuccess = false;
						},
						f:function(){
							$tipsPic[0].className='tips-pic pic-success';
							$tipsText.html('');
							this.isSuccess = true;
							$(me).prop('isOk',true);
							return true;
						},
						c:function(){
							this.val = val ;	//绑定该用户名可用，再次触发事件值不变的话不用再次查询
						},
						scope:me
					})
				}else{
					if(this.isSuccess){
						$tipsPic[0].className='tips-pic pic-success';
						$tipsText.html('');
					}else{
						$tipsPic[0].className='tips-pic pic-error';
						$tipsText.html(' 该用户名已被注册');
					}
				}
			}else{
				$tipsPic[0].className = 'tips-pic pic-warn';
				$tipsText.html(' 用户名格式不对');
			}
		}else{
			$tipsPic[0].className='tips-pic pic-warn';
			$tipsText.html(' 用户名不能为空');
		}
		$(me).prop('isOk',false);
		return false;
	}
	//邮箱校验函数
	var emailBlurCheck = function(){
		var me = this,
			$bcInput = $(me).closest('.bc-input');
		if($bcInput.children('.check-tips').length==0){
			focusCheck.call(me);
		}
		var	$tipsPic = $bcInput.find('.tips-pic'),
			$tipsText = $bcInput.find('.tips-text');
			val = $email.val();
		//如果邮箱存在前后空格，去除空格
		if(val!='' && /^\s|\s$/g.test(val)){
			$email.val(val = $.trim(val));
		}
		if(val!=''){
			//检测邮箱是否合格
			if(BC.checkEmail(val)){
				//判断邮箱是否可用且是否跟上一次相同
				if(!me.val || me.val != val){
					//显示正在加载图标
					$tipsPic[0].className = 'loading-pic rel-top-3';
					$tipsText.html('');
					//检测邮箱名是否已存在
					BC.checkUserExecute({
						uid:val,
						s:function(){
							$tipsPic[0].className='tips-pic pic-error';
							$tipsText.html(' 该邮箱已被注册');
							this.isSuccess = false;
						},
						f:function(){
							$tipsPic[0].className='tips-pic pic-success';
							$tipsText.html('');
							this.isSuccess = true;
							$(me).prop('isOk',true);
							return true;
						},
						c:function(){
							this.val = val ;	//绑定该邮箱可用，再次触发事件值不变的话不用再次查询
						},
						scope:me
					})
				}else{
					if(this.isSuccess){
						$tipsPic[0].className='tips-pic pic-success';
						$tipsText.html('');
					}else{
						$tipsPic[0].className='tips-pic pic-error';
						$tipsText.html(' 该邮箱已被注册');
					}
				}
			}else{
				$tipsPic[0].className = 'tips-pic pic-warn';
				$tipsText.html(' 邮箱格式不对');
			}
		}else{
			$tipsPic[0].className='tips-pic pic-warn';
			$tipsText.html(' 邮箱不能为空');
		}
		$(me).prop('isOk',false);
		return false;
	}
	//密码校验函数
	var pswBlurCheck = function(){
		var me = this,
			$bcInput = $(me).closest('.bc-input');
		if($bcInput.children('.check-tips').length==0){
			focusCheck.call(me);
		}
		var	$tipsPic = $bcInput.find('.tips-pic'),
			$tipsText = $bcInput.find('.tips-text');
			val = $(me).val();
		if(val!='' && /\s+/g.test(val)){
			$(me).val(val = BC.trimAll(val));
		}
		if(val!=''){
			if(me.className.indexOf("confirmpsw")!=-1){
				if(val!=BC.trimAll($password.val())){
					$tipsPic[0].className='tips-pic pic-warn';
					$tipsText.html(' 两次密码输入不一致');
				}else{
					$tipsPic[0].className='tips-pic pic-success';
					$tipsText.html('');
					$(me).prop('isOk',true);
					return true;
				}
			}else if(BC.checkPassword(val)){
				$tipsPic[0].className='tips-pic pic-success';
				$tipsText.html('');
				$(me).prop('isOk',true);
				return true;
			}else{
				$tipsPic[0].className='tips-pic pic-warn';
				$tipsText.html(' 密码长度不对');
			}
		}else{
			$tipsPic[0].className='tips-pic pic-warn';
			$tipsText.html(' 密码不能为空');
		}
		$(me).prop('isOk',false);
		return false;
	}
	//验证码校验函数
	var randBlurCheck = function(){
		var me = this,
			$bcInput = $(me).closest('.bc-input');
		if($bcInput.children('.check-tips').length==0){
			focusCheck.call(me);
		}
		var	$tipsPic = $bcInput.find('.tips-pic'),
			$tipsText = $bcInput.find('.tips-text');
			val = $(me).val();
		//如果邮箱存在前后空格，去除空格
		if(val!='' && /^\s|\s$/g.test(val)){
			$(me).val(val = $.trim(val));
		}
		if(val!=''){
			if(!me.val || me.val!=val){
				BC.checkRand({
					rand:val,
					s:function(){
						$tipsPic[0].className='tips-pic pic-success';
						$tipsText.html('');
						this.isSuccess = true;
						$(me).prop('isOk',true);
						return true;
					},
					e:function(){
						$tipsPic[0].className='tips-pic pic-error';
						$tipsText.html(' 验证码输入错误');
						this.isSuccess = false;
					},
					c:function(){
						this.val = val ;	//绑定验证码的值，用于判断下次有没有变动
					},
					scope:me
				});
			}
		}else{
			$tipsPic[0].className='tips-pic pic-warn';
			$tipsText.html(' 验证码不能为空');
		}
		$(me).prop('isOk',false);
		return false;
	}
	
	//绑定用户名校验事件
	$uid.focus(focusCheck);
	$uid.blur(uidBlurCheck);
	//绑定邮箱校验事件
	$email.focus(focusCheck);
	$email.blur(emailBlurCheck);
	//绑定密码校验事件
	$password.focus(focusCheck);
	$password.blur(pswBlurCheck);
	$password.keyup(pswBlurCheck);
	$confirmpsw.blur(pswBlurCheck);
	//绑定验证码校验事件
	$rand.blur(randBlurCheck);
	//刷新验证码
	$myImageRefresh.click(function(){
		$myImage.attr('src','/bookcrossing/public/image.jsp?'+new Date().getTime());
	});
	$form.submit(function(e){
		e.preventDefault();
		if(!!$uid.prop('isOk') && !!$email.prop('isOk') && !!$password.prop('isOk') && !!$confirmpsw.prop('isOk') && !!$rand.prop('isOk')){
			$('.password-md5').val(hex_md5($password.val())).val(); //秘密md5加密
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/reg',
				data:$form.serialize(),
				b:function(){
					$regBtn.html('<img src="/bookcrossing/img/loading.gif" />').attr('disabled','disabled');
				},
				s:function(resp){
					$tips.html(resp.msg+'，页面将在 <span class="countdown">3</span> 秒后跳转至首页，如不能跳转，请点击<a href="/bookcrossing/">首页</a>');
					tipsPic.className="tips-pic pic-success";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
					var i = 2 ,
						$countdown = $tips.find('.countdown'),
						timer = window.setInterval(function(){
							if(i<=0){
								window.clearTimeout(timer);
								window.location.href = BC.getRootPath();
							}
							$countdown.html(i--);
						},1000);
				},
				f:function(resp){
					$tips.html(resp.msg+'，请重新填写提交');
					tipsPic.className="tips-pic pic-error";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
					window.setTimeout(function(){$dialogBox.modal('hide')},1000);
				}
			});
		}else{
			if(!$uid.prop('isOk')){
				uidBlurCheck.call($uid[0]);
			}
			if(!$email.prop('isOk')){
				emailBlurCheck.call($email[0]);
			}
			if(!$password.prop('isOk')){
				pswBlurCheck.call($password[0]);
			}
			if(!$password.prop('isOk')){
				pswBlurCheck.call($confirmpsw[0]);
			}
			if(!$rand.prop('isOk')){
				randBlurCheck.call($rand[0]);
			}
		}
		
	});
})